package com.bdcom.autotest.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.bdcom.autotest.interaction.socket.DevSocket;

public class CUtil {
	
	private static Logger log = LoggerFactory.getLogger(CUtil.class);
	
	public static final String RES = "res";
	public static final String SID = "sid";
	
	public static RestTemplate getRestTemp() {
		
		RestTemplate bean = SysSpringContext.getBean(RestTemplate.class);
		
		return bean;
		
	}
	
	
	public static void threadsleep(long  ms ) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			log.error("" , e);
		}
	}
	
	
	public static String SESSIONID = "NULL";
	public static JSONObject httpPost( JSONObject args , String reqUrl  ) {
		
		RestTemplate restTemp = CUtil.getRestTemp();
		String serverurl ="http://"+ Prop.serverIP + ":"+Prop.serverPort;
		String absUrl = serverurl + reqUrl;

		String argsStr = args.toString();

		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.add("JSESSIONID", SESSIONID);
		requestHeaders.add("X-Requested-With", "XMLHttpRequest" );

		MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
		map.set("datas", argsStr);

		HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(map, requestHeaders);

		String retObj = restTemp.postForObject(absUrl, requestEntity, String.class);

		JSONObject ret = JSONObject.parseObject(retObj);
		
		return ret;
	}
	
	
	/**
	 * 合并数组
	 * 
	 * @param firstArray
	 *            第一个数组
	 * @param secondArray
	 *            第二个数组
	 * @return 合并后的数组
	 */
	public static byte[] concat(byte[] firstArray, byte[] secondArray) {
		if (firstArray == null || secondArray == null) {
			return null;
		}
		byte[] bytes = new byte[firstArray.length + secondArray.length];
		System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
		System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
		return bytes;
	}
	
	
	public static void showErrLog(String err) {
		System.err.println(  err );
	}
	
	public static void showLog(String log ) {
		System.out.println(  log  );
	}


	public static String getDateStr() {
		
		Calendar instance = Calendar.getInstance();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
		return sdf.format(instance.getTime());
	}
	
	public static String getTimeStr() {
		
		Calendar instance = Calendar.getInstance();
		String strDateFormat = "HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
		return sdf.format(instance.getTime());
	}
	
	
}
